# -*- coding: utf-8 -*-
"""
-------------------------------------------------------------------------------
File    : write_excel.py
Time    : 2023/8/7 14:09
author  : mojin
Email   : 397135766@qq.com
-------------------------------------------------------------------------------
"""

from openpyxl.styles import Alignment
from openpyxl.styles import Border,Side
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font  # 导入字体模块
import openpyxl  # openpyxl引入模块
from common.logger import Logger
from openpyxl.styles import PatternFill
class WriteExcel():
    def __init__(self,table_info):

        Logger.info(table_info)
        #宽，高，字体大小，字体颜色，字体粗体，表名，背景颜色，边框颜色
        self.width,  self.height, self.size,self.font_color,self.bold,self.sheetSt,self.fille_color,self.border_color,self.path, =table_info  # 分解题(后缀带_FJ)
        #self.font_style,
        #数据写入Excel
    def write_to_excel(self,writeData):#数据写入Excel
        #     实例化一个workbook对象
        workbook = openpyxl.Workbook()
        ws = workbook[workbook.sheetnames[0]]
        # 激活一个sheet
        sheet = workbook.active
        # 为sheet设置一个title
        sheet.title = self.sheetSt

        # 添加表头（不需要表头可以不用加）
        #data.insert(0, list(info))
        # 开始遍历数组

        for row_index, row_item in enumerate(writeData):
            # 设置边框
            border = Border(left=Side(border_style='thin',color=self.border_color),right=Side(border_style='thin',color=self.border_color),top=Side(border_style='thin',color=self.border_color),bottom=Side(border_style='thin',color=self.border_color))#设置边框
            # 设置字体样式
            font = Font(u'微软雅黑', size=self.size, bold=self.bold, italic=False, strike=False, color=self.font_color,)  # 设置字体样式
            fille = PatternFill('solid', fgColor=self.fille_color)  # 设置填充颜色为 橙色



            for col_index, col_item in enumerate(row_item):
                # 设置居中
                align = Alignment(horizontal=col_item[1], vertical=col_item[2], wrap_text=True)  # 设置居中  #center
                # 写入
                sheet.cell(row=row_index + 1, column=col_index + 1, value=col_item[0]).alignment = align  #设置居中
                sheet.cell(row=row_index + 1, column=col_index + 1, value=col_item[0]).font =font # 序列
                sheet.cell(row=row_index + 1, column=col_index + 1, value=col_item[0]).border = border#设置边框
                sheet.cell(row=row_index + 1, column=col_index + 1, value=col_item[0]).fill = fille  # 背景色


        ###修改行距 列距
        # width = 16
        # height = 36
        #Logger.info("row:", ws.max_row, "column:", ws.max_column)
        for i in range(1, ws.max_row + 1):
            ws.row_dimensions[i].height = self.height


        for i in range(1, ws.max_column + 1):
            ws.column_dimensions[get_column_letter(i)].width = self.width

        # 写入excel文件 如果path路径的文件不存在那么就会自动创建
        Logger.info(self.path)
        workbook.save(self.path)
        # try:
        #     workbook.save(self.path)
        #     Logger.success('写入成功')
        # except Exception as e:
        #     Logger.error(f'写入excle文件异常（{e}）')